#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <tuple>
#include <vector>
int n;
std::vector<std::tuple<int, int>> v;
int subtask()
{
    std::sort(v.begin(), v.end());
    // Subtask 1
    if (n <= 4000)
    {
        return 1;
    }
    // Subtask 2
    int minX, maxX, minY, maxY;
    minX = maxX = std::get<0>(v[0]);
    minY = maxY = std::get<1>(v[0]);
    for (const auto &[x, y] : v)
    {
        minX = std::min(minX, x);
        maxX = std::max(maxX, x);
        minY = std::min(minY, y);
        maxY = std::max(maxY, y);
    }
    if (1ll * (maxX - minX + 1) * (maxY - minY + 1) == n)
    {
        return 2;
    }
    // Subtask 3
    int l, r;
    l = -5e5 + 1;
    r = -5e5 + 1;
    auto it = v.begin();
    bool suc = true;
    for (; it != v.end() && suc;)
    {
        int cur = std::get<0>(*it);
        int cl, cr;
        cl = std::get<1>(*it);
        cr = cl;
        for (;;)
        {
            it++;
            if (it == v.end() || std::get<0>(*it) != cur)
            {
                break;
            }
            if (cr + 1 != std::get<1>(*it))
            {
                suc = false;
                break;
            }
            else
            {
                cr++;
            }
        }
        if (!(l <= cl && r <= cr))
        {
            suc = false;
            break;
        }
        l = cl;
        r = cr;
    }
    if (suc)
    {
        return 3;
    }
    // Subtask 4
    suc = true;
    std::map<std::tuple<int, int>, bool> map;
    for (const auto &[x, y] : v)
    {
        map[{x, y}] = true;
    }
    for (const auto &[x, y] : v)
    {
        if (map.count({x + 1, y}) >= 1 && map.count({x, y + 1}) >= 1 && map.count({x + 1, y + 1}) >= 1)
        {
            suc = false;
            break;
        }
    }
    if (suc)
    {
        return 4;
    }
    return 5;
}
namespace Subtask1
{
constexpr int MaxN = 4e3 + 5;
int dis[MaxN];
long long answer;
std::queue<int> queue;
std::vector<int> g[MaxN];
std::map<std::tuple<int, int>, int> map;
void bfs(int u)
{
    std::fill(dis + 1, dis + 1 + n, 0x3f3f3f3f);
    dis[u] = 0;
    queue.push(u);
    for (; !queue.empty();)
    {
        int u = queue.front();
        queue.pop();
        for (const auto &v : g[u])
        {
            if (dis[u] + 1 < dis[v])
            {
                dis[v] = dis[u] + 1;
                queue.push(v);
            }
        }
    }
}
int main()
{
    int index = 0;
    for (const auto &[x, y] : v)
    {
        map[{x, y}] = ++index;
    }
    for (const auto &[x, y] : v)
    {
        if (map.count({x + 1, y}) >= 1)
        {
            int u, v;
            u = map[{x, y}];
            v = map[{x + 1, y}];
            g[u].push_back(v);
            g[v].push_back(u);
        }
        if (map.count({x, y + 1}) >= 1)
        {
            int u, v;
            u = map[{x, y}];
            v = map[{x, y + 1}];
            g[u].push_back(v);
            g[v].push_back(u);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        bfs(i);
        for (int j = i + 1; j <= n; j++)
        {
            answer += dis[j];
        }
    }
    printf("%lld\n", answer);
    return 0;
}
} // namespace Subtask1
namespace Subtask3
{
constexpr int MaxN = 5e5 + 5;
int cnt[MaxN];
long long answer;
std::vector<std::tuple<int, int>> cv;
int main()
{
    auto it = v.begin();
    bool suc = true;
    for (; it != v.end() && suc;)
    {
        int cur = std::get<0>(*it);
        int cl, cr;
        cl = std::get<1>(*it);
        cr = cl;
        for (;;)
        {
            it++;
            if (it == v.end() || std::get<0>(*it) != cur)
            {
                break;
            }
            cr++;
        }
        cv.push_back({cl, cr});
    }
    int l, r;
    l = r = 0;
    int still = n;
    for (const auto &[cl, cr] : cv)
    {
        int m = cr - cl + 1;
        for (int i = l; i < cl; i++)
        {
            answer += cnt[i] * (cl - i);
            cnt[cl] += cnt[i];
            cnt[i] = 0;
        }
        still -= m;
        answer += 1ll * (n - still) * still;
        for (int i = cl; i <= cr; i++)
        {
            int pl, pr;
            pl = i - cl;
            pr = cr - i;
            answer += 1ll * cnt[i] * (pl + pr);
        }
        for (int i = 1; i <= m; i++)
        {
            answer += 1ll * i * (m - i);
        }
        for (int i = cl; i <= cr; i++)
        {
            cnt[i]++;
        }
        l = cl;
        r = cr;
    }
    printf("%lld\n", answer);
    return 0;
}
} // namespace Subtask3
namespace Subtask4
{
constexpr int MaxN = 5e5 + 5;
int n;
long long answer;
long long ds[MaxN];
long long up[MaxN];
long long down[MaxN];
std::vector<int> g[MaxN];
std::map<std::tuple<int, int>, int> map;
void dfsInit(int u, int f)
{
    ds[u]++;
    for (const auto &v : g[u])
    {
        if (v == f)
        {
            continue;
        }
        ds[u] += ds[v];
        down[u] += down[v] + ds[v];
    }
}
void dfs(int u, int f, long long upon)
{
    answer += upon + down[u];
    for (const auto &v : g[u])
    {
        if (v == f)
        {
            continue;
        }
        long long ad = upon;
        upon -= down[v] + ds[v];
        ad += n - ds[v];
        dfs(v, u, ad);
    }
}
int main()
{
    int index = 0;
    for (const auto &[x, y] : v)
    {
        map[{x, y}] = ++index;
    }
    for (const auto &[x, y] : v)
    {
        if (map.count({x + 1, y}) >= 1)
        {
            int u, v;
            u = map[{x, y}];
            v = map[{x + 1, y}];
            g[u].push_back(v);
            g[v].push_back(u);
        }
        if (map.count({x, y + 1}) >= 1)
        {
            int u, v;
            u = map[{x, y}];
            v = map[{x, y + 1}];
            g[u].push_back(v);
            g[v].push_back(u);
        }
    }
    dfsInit(1, 0);
    dfs(1, 0, 0);
    printf("%lld\n", answer);
    return 0;
}
}; // namespace Subtask4
int main()
{
    freopen("game.in", "r", stdin);
    freopen("game.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        v.push_back({x, y});
    }
    int sub = subtask();
    if (sub == 1)
    {
        Subtask1::main();
    }
    if (sub == 2 || sub == 3)
    {
        Subtask3::main();
    }
    if (sub == 4)
    {
        Subtask4::main();
    }
    return 0;
}